package com.qing.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.eduservice.entity.dto.SubjectNestedDto;
import com.qing.eduservice.entity.excel.ExcelSubjectData;
import com.qing.eduservice.entity.pojo.EduSubject;
import com.qing.eduservice.entity.dto.SubjectDto;
import com.qing.eduservice.listen.SubjectExcelListener;
import com.qing.eduservice.mapper.EduSubjectMapper;
import com.qing.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.servicebase.exception.JsonException;
import com.qing.servicebase.utils.ResponseResult;
import com.qing.servicebase.utils.ResponseState;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-09-08
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    //poi读取excel内容
    @Override
    public ResponseResult batchImport(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
//1 获取文件输入流
            InputStream inputStream = file.getInputStream();
// 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubjectData.class, new
                    SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new JsonException(202, "添加课程分类失败");
        }
        return ResponseResult.ofSuccess(ResponseState.OK);
    }

    @Override
    public ResponseResult nestedList() {
        //获取一级分类数据记录
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<EduSubject> oneSubject = baseMapper.selectList(queryWrapper);
        //获取二级分类数据记录
        QueryWrapper<EduSubject> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ne("parent_id", 0);
        List<EduSubject> twoSbject = baseMapper.selectList(queryWrapper2);
        //填充一级分类Dto数据
        //最终要的到的数据列表
        List<SubjectNestedDto> finalSubjectList = new ArrayList<>();
        for (int i = 0; i < oneSubject.size(); i++) {
            //创建一级类别Dto对象
            EduSubject eduSubject = oneSubject.get(i);
            SubjectNestedDto subjectNestedDto = new SubjectNestedDto();
            BeanUtils.copyProperties(eduSubject, subjectNestedDto);
            finalSubjectList.add(subjectNestedDto);
            //填充二级分类Dto数据
            List<SubjectDto> subjectDtoArrayList = new ArrayList<SubjectDto>();
            for (int j = 0; j < twoSbject.size(); j++) {
                EduSubject subjects = twoSbject.get(j);
                if (subjects.getParentId().equals(eduSubject.getId())) {
                    //创建二级类别Dto对象
                    SubjectDto subjectDto = new SubjectDto();
                    BeanUtils.copyProperties(subjects, subjectDto);
                    subjectDtoArrayList.add(subjectDto);
                }
            }
            subjectNestedDto.setChildren(subjectDtoArrayList);
        }
        return ResponseResult.ofSuccess(finalSubjectList);
    }
}
